package cn.jiyun;

import cn.jiyun.pojo.User;
import cn.jiyun.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Fay
 * @date 2022/6/22&16:23
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class test {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void test() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        //获取缓存中的userList
        String userList =(String) redisTemplate.boundValueOps("user_list").get();
        //如果缓存中不存在的话就重新去数据库中查询
        if (StringUtils.isEmpty(userList)){
            List<User> list = userService.findAll();
            userList = objectMapper.writeValueAsString(list);
            redisTemplate.boundValueOps("user_list").set(userList);
            System.out.println("从数据库中获取数据");
            System.out.println(userList);
        }else {
            System.out.println("从缓存中获取数据");
            System.out.println(userList);
        }
    }

    //redis存储string类型
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Test
    public void stringRedis(){
        stringRedisTemplate.boundValueOps("string").set("张三");
    }

    //操作hash类型
    @Test
    public void hashRedis(){
        HashMap<String, String> map = new HashMap<>();
        map.put("username","张三");
        map.put("age","15");
        map.put("sex","男");
        for (String s : map.keySet()) {
            String o = map.get(s);
            stringRedisTemplate.boundHashOps("hash").put(s,o);
        }
    }
    //操作list类型
    @Test
    public void listRedis(){
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        for (String s : list) {
            stringRedisTemplate.boundListOps("list").leftPush(s);
        }
    }
    //操作set类型
    @Test
    public void setRedis(){
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        for (String s : list) {
            stringRedisTemplate.boundSetOps("set").add(s);
        }
    }
    //操作zset
    @Test
    public void zsetRedis(){
        HashMap<String, Double> map = new HashMap<>();
        map.put("张三",100.0);
        map.put("李四",10.0);
        map.put("王五",50.0);
        map.put("赵六",60.1);
        for (String s : map.keySet()) {
            Double o  = map.get(s);
            stringRedisTemplate.boundZSetOps("zset").add(s,o);
        }

    }
}
